import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';

void main() {
  runZoned(() {
    //在runApp之前确保BoostFlutterBinding初始化
    CustomFlutterBinding();
    runApp(MyApp());
  });
}

class CustomFlutterBinding extends WidgetsFlutterBinding with BoostFlutterBinding {}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  ///路由表
  static Map<String, FlutterBoostRouteFactory> routerMap = {
    'homePage': (settings, uniqueId) {
      return CupertinoPageRoute(
          settings: settings,
          builder: (_) {
            Map<String, Object> map = settings.arguments as Map<String, Object> ;
            String data = map['data'] as String;
            return MainPage(
              data: data,
            );
          });
    },
    'simplePage': (settings, uniqueId) {
      return CupertinoPageRoute(
          settings: settings,
          builder: (_) {
            Map<String, Object> map = settings.arguments as Map<String, Object>;
            String data = map['data'] as String;
            return SimplePage(
              data: data,
            );
          });
    },
  };

  Route<dynamic>? routeFactory(RouteSettings settings, String? uniqueId) {
    FlutterBoostRouteFactory func = routerMap[settings.name] as FlutterBoostRouteFactory;
    return func(settings, uniqueId);
  }

  Widget appBuilder(Widget home) {
    return MaterialApp(home: home,
    debugShowCheckedModeBanner: true,
    builder: (_,__){
      return home;
    },);
  }

  @override
  Widget build(BuildContext context) {
    return FlutterBoostApp(routeFactory,appBuilder: appBuilder,);
  }

}

class MainPage extends StatelessWidget {

  MainPage({Object? data});

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(child: Text("This is main page."),),
    );
  }
}

class SimplePage extends StatelessWidget {
  SimplePage({Object? data});

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(child: Text("This is simple page."),),
    );
  }
}